<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\News;
use App\Jobs\SendReminderNew;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class NewController extends Controller
{
    protected $news;
    //实例化类
    public function __construct(){
        $this->news = new News();
    }

    /**
     * 查询段子数据
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    function getNewData(){
        //会只取出软删除数据
        //$data['news'] = News::onlyTrashed();
        //强制查询已被软删除的模型
        //$data['news'] = News::withTrashed();
        $cacheKey = 'page:admin:news_index';
        $pageCache = Redis::get($cacheKey);
//        if(!$pageCache) {
            $data['news'] =$this->news->where('type','=',1)
                ->orderBy('created_at', 'src')
                ->take(10)
                ->get()->toArray();
            foreach ($data['news'] as $key=>$value){
                $data['news'][$key]['avatar'] = image_file_exists($value['avatar']);
            }
            $view =  view('Admin.new.index',$data);
            Redis::set($cacheKey, $view);
            return $view;
//        }
//        return $pageCache;
    }

    /**
     * 查询图片数据
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    function getPicData(){
        $cacheKey = 'page:admin:pic_index';
        $pageCache = Redis::get($cacheKey);
//        if(!$pageCache) {
            $data['pic'] =News::where('type','=',2)
                ->orderBy('created_at', 'src')
                ->take(10)
                ->get()->toArray();
            foreach ($data['pic'] as $key=>$value){
                $data['pic'][$key]['avatar'] = image_file_exists($value['avatar']);
                $data['pic'][$key]['pic_url'] = image_file_exists($value['pic_url']);
            }
            $view = view('Admin.pic.index',$data);
            Redis::set($cacheKey, $view);
            return $view;
//        }
        return $pageCache;
    }

    /**
     * 运行bat文件，执行nodejs
     * @param Request $request
     */
    function beginBat(Request $request){
        if($request->get('type')=='duanzi'){
            exec('D:\xampp\htdocs\laravel5.4\public\bat\nodejs_duanzi.bat');
        }else{
            exec('D:\xampp\htdocs\laravel5.4\public\bat\nodejs_pic.bat');
        }
    }

    /**
     * 修改段子
     * @param Request $request
     * @return array
     */
    function updateNew(Request $request){
        $news_id = $request->get('news_id');
        $content = $request->get('content');

        if($news_id==''){
            abort(404);
        }
        $obj = News::find($news_id);
        $obj->content = $content;
        $obj->save();
        return $obj==true?success('更新成功',$obj):error('更新失败');
    }

    /**
     * 删除段子
     * @param $new_id
     * @return mixed
     */
    public function deleteNew($new_id){
        if($new_id==''){
            abort(404);
        }
        $obj = News::find($new_id);
        $obj->deleted_at = date('Y-m-d H:i:s');
        $obj->save();
        return $obj==true?success('删除成功',$obj):error('删除失败');
    }




    function test(){
        // 发送请求
        $result = curl_url('http://www.laravel5.4.app:9999/');
        // 收到的数据需要转化一下
        $resultAry = json_decode($result);
        foreach ($resultAry as $ary){
            $obj = new News();
            $obj->name = $ary->name;
            $obj->data_group_id = $ary->data_group_id;
            $obj->avatar = $ary->avatar;
            $obj->date = $ary->date;
            $obj->title = $ary->title;
            $obj->content = $ary->content;
            $obj->praise = $ary->praise;
            $obj->comment = $ary->comment;
            $obj->reprint = $ary->reprint;
            $job = (new SendReminderNew($obj))->onQueue('news');
            $this->dispatch($job);
        }

    }
}
